package com.mango.game.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.mango.game.dtos.ListDTO;
import com.mango.game.dtos.QueryDTO;
import com.mango.game.dtos.NameSpaceDTO;
import com.mango.game.dtos.ResponseDTO;
import com.mango.game.entity.NameSpace;
import com.mango.game.mapper.NameSpaceMapper;
import com.mango.game.service.INameSpaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 命名空间 服务实现类
 * </p>
 *
 * @author Mango
 * @since 2025-02-24
 */
@Service
public class NameSpaceServiceImpl extends ServiceImpl<NameSpaceMapper, NameSpace> implements INameSpaceService {

    @Override
    public ResponseDTO customSave(NameSpaceDTO dto) {
        ResponseDTO resp = new ResponseDTO();

        NameSpace nameSpace = baseMapper.selectOne(new LambdaQueryWrapper<NameSpace>().eq(NameSpace::getName, dto.getName()));
        if (nameSpace != null) {
            resp.setCode(-1);
            resp.setMessage("保存失败，同名空间已经存在");
        }
        nameSpace = new NameSpace();
        nameSpace.setName(dto.getName());
        nameSpace.setLabel(dto.getLabel());
        nameSpace.setStatus(dto.getStatus());

        int len = baseMapper.insert(nameSpace); // 保存到数据库

        if (len > 0) {
            resp.setCode(0);
            resp.setMessage("保存成功");
            resp.setData(nameSpace);
        } else {
            resp.setCode(-1);
            resp.setMessage("保存失败，数据库执行失败");
        }
        return resp;
    }

    @Override
    public ResponseDTO customUpdate(NameSpaceDTO dto) {
        ResponseDTO resp = new ResponseDTO();

        NameSpace nameSpace = baseMapper.selectById(Long.parseLong(dto.getUid()));
        if (nameSpace == null) {
            resp.setCode(-1);
            resp.setMessage("更新失败，对象不存在");
            return resp;
        }
        nameSpace.setName(dto.getName());
        nameSpace.setLabel(dto.getLabel());
        nameSpace.setStatus(dto.getStatus());

        int len = baseMapper.updateById(nameSpace);

        if (len > 0) {
            resp.setCode(0);
            resp.setMessage("更新成功");
            resp.setData(nameSpace);
        } else {
            resp.setCode(-1);
            resp.setMessage("更新失败，数据库执行失败");
        }
        return resp;
    }

    @Override
    public ResponseDTO customDelete(NameSpaceDTO dto) {
        ResponseDTO resp = new ResponseDTO();

        NameSpace nameSpace = baseMapper.selectById(Long.parseLong(dto.getUid()));
        if (nameSpace == null) {
            resp.setCode(-1);
            resp.setMessage("删除失败，对象不存在");
            return resp;
        }

        int len = baseMapper.deleteById(Long.parseLong(dto.getUid()));

        if (len > 0) {
            resp.setCode(0);
            resp.setMessage("删除成功");
            resp.setData(nameSpace);
        } else {
            resp.setCode(-1);
            resp.setMessage("删除失败，数据库执行失败");
        }
        return resp;
    }

    @Override
    public ResponseDTO customList(ListDTO<NameSpace> dto) {
        IPage<NameSpace> page = new PageDTO<>(dto.getCurrent(), dto.getSize());
        IPage<NameSpace> data = baseMapper.selectPage(page, new QueryWrapper<>());

        return new ResponseDTO(data);
    }

    @Override
    public ResponseDTO customQuery(QueryDTO<NameSpace> dto) {
        IPage<NameSpace> page = new PageDTO<>(dto.getCurrentPage(), dto.getPageSize());
        IPage<NameSpace> pages = baseMapper.onCustomQueryPage(page, dto);

        return new ResponseDTO(pages);
    }


}
